package com.saret.translate;

import com.saret.translate.type.MyDictionary;
import com.saret.translate.type.TextEntity;
import com.saret.utils.FileLocator;
import com.saret.utils.GeezUtil;
import java.io.File;
import java.io.FileFilter;

/**
 * @author biniam.gebremichael
 * @since 11/25/11
 */
public class Translator {
    static File files = new File("C:\\perl\\74.63.78.28\\2011\\november");

    public static void main(String[] args) {
        TextEntity english;
        MyDictionary myDictionary = new MyDictionary(new File(FileLocator.getTranslateDir(), "dictionary.csv"));
        for (File en : files.listFiles(new Filter("eritrea_profile.*\\.txt"))) {
            english = new TextEntity(en);
            for (String line : english.getLines()) {
                String[] englishWords = line.split("\\s+");
                MyDictionary enSmallMyDictionary = myDictionary.translate(englishWords);
                if (enSmallMyDictionary.getDictionary().size() > 20 && (double) enSmallMyDictionary.getDictionary().size() / englishWords.length > 0.5) {
                    System.out.println(" GOOD" + line);
                    for (File tiFile : files.listFiles(new Filter("haddas.*\\.txt"))) {
                        findTranslation(englishWords, enSmallMyDictionary, tiFile);
                    }
                }

            }
        }
    }

    private static void findTranslation(String[] englishWords, MyDictionary enSmallMyDictionary, File tiFile) {
        TextEntity tigrigna = new TextEntity(tiFile);
        for (String tiLines : tigrigna.getLines()) {
            String[] tiWords = GeezUtil.cleanGeez(tiLines).split("\\s+");
            if (makesSenseBySize(englishWords.length, tiWords.length)) {
                MyDictionary tiSmallMyDictionary = enSmallMyDictionary.reverseTranslate(tiWords);
                if (makesSenseBySize(enSmallMyDictionary.getDictionary().size(), tiSmallMyDictionary.getDictionary().size())) {
                    System.out.println(tiSmallMyDictionary.getDictionary().size() + " Translation " + tiLines);
                }
            }
        }
    }

    private static boolean makesSenseBySize(int englishWords, int tiWords) {
        return 2 * tiWords > englishWords && tiWords < englishWords * 2;
    }


    static class Filter implements FileFilter {
        String regex;

        Filter(String regex) {
            this.regex = regex;
        }

        @Override
        public boolean accept(File pathname) {
            return pathname.getName().matches(regex);
        }
    }

}
